package de.jmda.gen;

import static de.jmda.util.StringUtil.sb;
import static org.junit.Assert.*;

import org.junit.Test;

public class JUT_LineIndenter
{
	@Test
	public void testLineIndenterStringIntBoolean()
	{
		String indentation = "x";
		int indentationLevel = 3;
		boolean indenting = true;

		LineIndenter lineIndenter =
				new LineIndenter(indentation, indentationLevel, indenting);

		// check state of lineIndenter after construction
		assertEquals(
				"indentation", indentation, lineIndenter.getIndentation());
		assertEquals(
				"indentation level", indentationLevel, lineIndenter.getIndentationLevel());
		assertEquals(
				"indenting", indenting, lineIndenter.getIndenting());

		// null should be treated as ""
		indentation = null;

		lineIndenter =
				new LineIndenter(indentation, indentationLevel, indenting);
		
		assertEquals(
				"indentation", "", lineIndenter.getIndentation());

		// negative value should be treated as 0
		indentationLevel = -3;

		lineIndenter =
				new LineIndenter(indentation, indentationLevel, indenting);
		
		assertEquals(
				"indentation level", 0, lineIndenter.getIndentationLevel());
	}

	@Test
	public void testLineIndenter()
	{
		LineIndenter lineIndenter = new LineIndenter();

		// check state of lineIndenter after construction
		assertEquals("indentation", "", lineIndenter.getIndentation());
		assertEquals("indentation level", 0, lineIndenter.getIndentationLevel());
		assertEquals("indenting", false, lineIndenter.getIndenting());
	}

	@Test
	public void testSetIndentation()
	{
		LineIndenter lineIndenter = new LineIndenter();
		
		String indentation = "x";
		
		lineIndenter.setIndentation(indentation);
		
		assertEquals("indentation", "x", lineIndenter.getIndentation());

		// null should be treated as ""
		indentation = null;
		
		lineIndenter.setIndentation(indentation);
		
		assertEquals("indentation", "", lineIndenter.getIndentation());
	}

	@Test
	public void testSetIndentationLevel()
	{
		LineIndenter lineIndenter = new LineIndenter();
		
		int indentationLevel = 3;
		
		lineIndenter.setIndentationLevel(indentationLevel);
		
		assertEquals("indentation level", 3, lineIndenter.getIndentationLevel());

		// negative values should be treated as 0
		indentationLevel = -3;
		
		lineIndenter.setIndentationLevel(indentationLevel);
		
		assertEquals("indentation level", 0, lineIndenter.getIndentationLevel());
	}

	@Test
	public void testIndentString()
	{
		// should not indent because indent level is 0 and indenting is false
		LineIndenter lineIndenter = new LineIndenter();
		
		String noLines = "noLines";
		
		assertEquals("no lines", noLines, lineIndenter.indent(noLines));
		
		// should not indent because indent level is 0
		lineIndenter.setIndenting(true);
		
		assertEquals("no lines", noLines, lineIndenter.indent(noLines));
		
		// should not indent because indentation is ""
		lineIndenter.setIndentationLevel(1);

		assertEquals(
				"no lines",
				lineIndenter.getIndentation() + noLines,
				lineIndenter.indent(noLines));
		
		// should indent because indentation is "x"
		lineIndenter.setIndentation("x");

		assertEquals(
				"no lines",
				lineIndenter.getIndentation() + noLines,
				lineIndenter.indent(noLines));
	}

	@Test
	public void testIndentStringBuffer()
	{
		// should not indent because indent level is 0 and indenting is false
		LineIndenter lineIndenter = new LineIndenter();
		
		StringBuffer noLines = sb("noLines");
		
		assertEquals("no lines", noLines, lineIndenter.indent(noLines));
		
		// should not indent because indent level is 0
		lineIndenter.setIndenting(true);
		
		assertEquals(
				"no lines",
				noLines.toString(),
				lineIndenter.indent(noLines).toString());
		
		// should not indent because indentation is ""
		lineIndenter.setIndentationLevel(1);

		assertEquals(
				"no lines",
				lineIndenter.getIndentation() + noLines,
				lineIndenter.indent(noLines).toString());
		
		// should indent because indentation is "x"
		lineIndenter.setIndentation("x");

		assertEquals(
				"no lines",
				lineIndenter.getIndentation() + noLines,
				lineIndenter.indent(noLines).toString());
	}
}